<template>
  <section class="todoapp">
    <!-- 头部部分 -->
    <TodoHeader @fn="fn"></TodoHeader>

    <!-- 主体部分 -->
    <TodoBady :list="renderlist" @delList="delList" @change="change"></TodoBady>

    <!-- 底部部分 -->
    <TodoFooter
      :listleng="listleng"
      @clearclick="clearclick"
      v-show="this.list.length"
      :type="type"
      @changetype="changetype"
      :isDoneAll="isDoneAll"
    ></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from '@/components/TodoHeader.vue'
import TodoBady from '@/components/TodoBady.vue'
import TodoFooter from '@/components/TodoFooter.vue'

export default {
  data() {
    return {
      type: 'all',
      list: [
        { id: 1, name: '吃饭', isDone: true },
        { id: 2, name: '睡觉', isDone: false },
        { id: 3, name: '打豆豆', isDone: true },
      ],
    }
  },
  components: {
    TodoHeader,
    TodoBady,
    TodoFooter,
  },
  methods: {
    delList(id) {
      this.list = this.list.filter((item) => item.id !== id)
    },
    change(id) {
      const res = this.list.find((item) => item.id === id)
      res.isDone = !res.isDone
    },
    fn(shuru) {
      // console.log(shuru);
      const obj = { id: +new Date(), name: shuru, isDone: false }
      this.list = [obj, ...this.list]
    },
    clearclick() {
      this.list = this.list.filter((item) => !item.isDone)
    },
    changetype(type) {  
      this.type = type
    },
  },
  computed: {
    listleng() {
      return this.list.filter((item) => !item.isDone).length
    },
    isDoneAll() {
      return this.list.some((item) => item.isDone)
    },
    renderlist() {
      switch (this.type) {
        case 'all':
          return this.list
        case 'active':
          return this.list.filter(item=> !item.isDone)
        case 'completed':
          return this.list.filter(item=> item.isDone)

        default:
          this.list
      }
    },
  },
}
</script>

<style></style>
